package com.raising.framework.entity;

import com.raising.framework.config.JConfig;
import com.raising.modules.sys.entity.User;
import com.raising.utils.JStringUtils;
import com.raising.utils.UserUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.format.annotation.DateTimeFormat;

import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 数据实体
 * @author gaoyuan
 * @param <T>
 */
public abstract class BaseEntity<T>  implements Serializable {
    private static final long serialVersionUID = 1L;

    /** 实体编号（唯一标识）*/
    protected String id;
    /** 备注 */
    protected String remarks;
    /** 创建者 */
    protected String createBy;
    /** 创建日期 */
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    protected Date createDate;
    /** 更新者 */
    protected String updateBy;
    /** 更新日期 */
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    protected Date updateDate;
    /** 状态 */
    protected String status;
    /** 数据类型 */
    protected String dbType;
    /** 排序字段 */
    protected String orderBy;
    /** 筛选字段 */
    protected String limitBy;

    /** 自定义创建人对应字段 */
    protected String customCreateByStr;
    /** 组织机构父级编号列表 */
    protected String orgParentIds;
    /** 组织机构父级的父级编号列表 */
    protected String orgParentParentIds;

    /**
     * 是否是新记录（默认：false），调用setIsNew()设置新记录，使用自定义ID。
     * 设置为true后强制执行插入语句，ID不会自动生成，需从手动传入。
     */
    protected boolean isNewId = false;

    public BaseEntity() { }

    public BaseEntity(String id) {
        this();
        this.id = id;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getRemarks() {
        return remarks;
    }

    public void setRemarks(String remarks) {
        this.remarks = remarks;
    }

    public String getCreateBy() {
        return createBy;
    }

    public void setCreateBy(String createBy) {
        this.createBy = createBy;
    }

    public Date getCreateDate() {
        return createDate;
    }

    public void setCreateDate(Date createDate) {
        this.createDate = createDate;
    }

    public String getCreateDateStr() {
        try {
            return new SimpleDateFormat("yyyy-MM-dd").format(this.createDate);
        }catch(Exception e){
            return null;
        }
    }
    public void setCreateDateStr(String value) {
        try {
            this.createDate = new SimpleDateFormat("yyyy-MM-dd").parse(value);
        } catch (Exception e) {
        }
    }
    public String getUpdateBy() {
        return updateBy;
    }

    public void setUpdateBy(String updateBy) {
        this.updateBy = updateBy;
    }

    public Date getUpdateDate() {
        return updateDate;
    }

    public void setUpdateDate(Date updateDate) {
        this.updateDate = updateDate;
    }
    public String getUpdateDateStr() {
        try {
            return new SimpleDateFormat("yyyy-MM-dd").format(this.updateDate);
        }catch(Exception e){
            return null;
        }
    }
    public void setUpdateDateStr(String value) {
        try {
            this.updateDate = new SimpleDateFormat("yyyy-MM-dd").parse(value);
        } catch (Exception e) {
        }
    }
    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public boolean getIsNewId() {
        return isNewId || StringUtils.isBlank(getId());
    }

    public void setNewId(boolean isNewId) {
        this.isNewId = isNewId;
    }

    public String getDbType() {
        return JConfig.getConfig("jdbc.type");
    }

    public void setDbType(String dbType) {
        this.dbType = dbType;
    }

    public String getOrderBy() {
        return orderBy;
    }

    public void setOrderBy(String orderBy) {
        this.orderBy = orderBy;
    }

    public String getLimitBy() {
        return limitBy;
    }

    public void setLimitBy(String limitBy) {
        this.limitBy = limitBy;
    }

    public String getOrgParentIds() {
        return orgParentIds;
    }

    public void setOrgParentIds(String orgParentIds) {
        this.orgParentIds = orgParentIds;
    }

    public String getOrgParentParentIds() {
        return orgParentParentIds;
    }

    public void setOrgParentParentIds(String orgParentParentIds) {
        this.orgParentParentIds = orgParentParentIds;
    }

    public String getCustomCreateByStr() {
        return customCreateByStr;
    }

    public void setCustomCreateByStr(String customCreateByStr) {
        this.customCreateByStr = customCreateByStr;
    }

    /**
     * 插入之前执行方法，子类实现
     * @author gaoyuan
     * @param createBy 创建人或更新人
     * 可取值为null，则表示获取当前登录人的信息
     */
    public void preInsert(String createBy) {
        if(JStringUtils.isNotBlank(createBy)){
            this.updateBy = createBy;
            this.createBy = createBy;
        }else{
            User user = UserUtils.getLoginUser();
            if (user != null) {
                this.updateBy = user.getId();
                this.createBy = user.getId();
            }
        }
        this.createDate = new Date();
        this.updateDate = new Date();
    }
    /**
     * 更新之前执行方法，子类实现
     * @author gaoyuan
     * @param createBy 创建人或更新人
     * 可取值为null，则表示获取当前登录人的信息
     */
    public void preUpdate(String createBy) {
        if(JStringUtils.isNotBlank(createBy)){
            this.updateBy = createBy;
        }else{
            User user = UserUtils.getLoginUser();
            if (user != null) {
                this.updateBy = user.getId();
            }
        }
        this.updateDate = new Date();
    }

}

